/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.sprite.blit;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
import hapi.sprite.display.SpriteSheetDisplay;
import hapi.sprite.sheet.CopyPixelsSpriteSheet;
import hapi.sprite.sheet.DrawOnBitmapSpriteSheet;

class BlitBitmapSpriteSheetDisplay implements SpriteSheetDisplay {

	var spriteSheet:CopyPixelsSpriteSheet;
	var dest:Point;
	public var x:Float;
	public var y:Float;
	public var visible(default, null):Bool;
	public var currentFrame(default, null):Int;
	public var totalFrames(default, null):Int;
	
	public function new(spriteSheet:CopyPixelsSpriteSheet, ?x:Float=0.0, ?y:Float=0.0) {
		this.dest = new Point(x, y);
		this.x = x;
		this.y = y;
		this.spriteSheet = spriteSheet;
		this.totalFrames = spriteSheet.totalFrames;
		this.currentFrame = 1;
		this.visible = true;
	}
	
	public inline function isVisible():Bool {
		return visible;
	}
	
	public function show():Void {
		this.visible = true;
	}
	
	public function hide():Void {
		this.visible = false;
	}
	
	public function goto(frame:Int) {
		if(frame <= totalFrames && frame >= 1 && this.currentFrame != frame) {
			this.currentFrame = frame;
		}
	}
	
	public inline function renderOn(canvas:Bitmap) {
		if (isVisible()) {
			dest.x = x;
			dest.y = y;
			spriteSheet.drawSpriteOn(canvas, currentFrame, dest);
		}
	}
	
	public inline function moveTo(x:Float, y:Float):Void {
		this.x = x;
		this.y = y;
	}
}